#include "ticket.h"

std::ostream& operator<<(std::ostream& out, const Ticket& t) {
    out << "Name: " << t.name_ << std::endl;
    out << "Price: " << t.price_ << std::endl;
    out << "Quantity: " << t.quantity_ << std::endl;
    return out;
}

std::istream& operator>>(std::istream& in, Ticket& t) {
    char buffer[100];
    std::cout << "Enter ticket name: ";
    in.getline(buffer, 100);
    t.name_ = new char[strlen(buffer) + 1];
    strcpy(t.name_, buffer);
    std::cout << "Enter ticket price: ";
    in >> t.price_;
    std::cout << "Enter ticket quantity: ";
    in >> t.quantity_;
    in.ignore();
    return in;
}

Ticket::Ticket() : name_(nullptr), price_(0.0), quantity_(0) {}

Ticket::Ticket(const char* name, double price, int quantity)
    : name_(nullptr), price_(price), quantity_(quantity) {
    setName(name);
}

Ticket::Ticket(const Ticket& other)
    : name_(nullptr), price_(other.price_), quantity_(other.quantity_) {
    setName(other.name_);
}

Ticket& Ticket::operator=(const Ticket& other) {
    if (this != &other) {
        setName(other.name_);
        price_ = other.price_;
        quantity_ = other.quantity_;
    }
    return *this;
}

Ticket::~Ticket() {
    delete[] name_;
}

const char* Ticket::getName() const {
    return name_;
}

double Ticket::getPrice() const {
    return price_;
}

int Ticket::getQuantity() const {
    return quantity_;
}

void Ticket::setName(const char* name) {
    delete[] name_;
    name_ = new char[strlen(name) + 1];
    strcpy(name_, name);
}

void Ticket::setPrice(double price) {
    price_ = price;
}

void Ticket::setQuantity(int quantity) {
    quantity_ = quantity;
}

void Ticket::editTicket(Ticket& ticket) {
    std::cout << "Enter new ticket details: " << std::endl;
    std::cin >> ticket;
    std::cout << "Ticket details have been updated." << std::endl;
}

void Ticket::readTicketFromFile(Ticket& ticket) {
    std::ifstream file("ticket.txt");
    if (!file) {
        std::cout << "Unable to open ticket file. Creating new ticket." << std::endl;
        return;
    }

    file >> ticket;
    file.close();
    std::cout << "Ticket data has been read from file." << std::endl;
}

void Ticket::writeTicketToFile(const Ticket& ticket) {
    std::ofstream file("ticket.txt");
    if (!file) {
        std::cout << "Unable to open ticket file for writing." << std::endl;
        return;
    }

    file << ticket;
    file.close();
    std::cout << "Ticket data has been written to file." << std::endl;
}

void Ticket::printTicket(const Ticket& ticket) {
    std::cout << "Ticket details: " << std::endl;
    std::cout << ticket;
}



